package com.milk.supply.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;

/**
 *
 * Created by Rayn on 2015/12/16.
 */
@Controller
@RequestMapping("/test")
public class PageController
{

    @ResponseBody
    @RequestMapping("page.do")
    public HashMap<String, Object> testJSONPage(){
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put( "username", "zhongguo" );
        result.put( "success", true );

        return result;
    }

    @ResponseBody
    @RequestMapping("upload.do")
    public HashMap<String, Object> uploadFile(HttpServletRequest request, @RequestParam(value = "updateFile") MultipartFile updateFile){
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put( "username", "成功" );
        result.put( "success", true );

        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName = updateFile.getOriginalFilename();

        System.out.println(path);
        File targetFile = new File(path, fileName);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }

        //保存
        try {
            updateFile.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

}
